package com.wlper.lib.view.loading;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewStub;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.nearshop.R;
import com.wlper.lib.uitls.ViewUtils;

/**
 * 预设了加载界面和加载失败界面。但加载动画实现的方式和控制，需要子类去实现。
 * 
 * @author wanliang527
 * @date 2014-06-13
 * 
 * 
 */
public abstract class SimpleLoadLayout extends BaseLoadLayout {

    protected ViewStub loadingView, loadFailedView;// 加载中和加载失败的视图
    protected static View.OnClickListener layoutClickEvent; // 整个控件的点击事件，添加了重新加载事件
    protected View.OnTouchListener layoutTouchEvent;

    private boolean isTouchListenerValid = false;
    private int touchMoveCounts = 0;
    private boolean clickStartFlag = false;

    public SimpleLoadLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SimpleLoadLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SimpleLoadLayout(Context context) {
        super(context);
    }

    /**
     * 初始化操作。添加了加载中和加载失败的视图
     */
    @Override
    protected void init(Context context) {
        super.init(context);
        layoutClickEvent = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onReload();
            }
        };
        layoutTouchEvent = new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        clickStartFlag = true;
                        break;

                    case MotionEvent.ACTION_MOVE:
                        if (clickStartFlag) {
                            if (++touchMoveCounts > 5) {
                                clickStartFlag = false;
                                touchMoveCounts = 0;
                            }
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        if (clickStartFlag) {
                            onReload();
                            clickStartFlag = false;
                            touchMoveCounts = 0;
                        }
                        break;
                }
                return true;
            }
        };

        loadingView = new ViewStub(mContext);
        loadingView.setLayoutResource(R.layout.wl_view_loading_in_bg);
        this.addView(loadingView);
        ((FrameLayout.LayoutParams) loadingView.getLayoutParams()).gravity = Gravity.CENTER;

        loadFailedView = new ViewStub(mContext);
        loadFailedView.setLayoutResource(R.layout.wl_view_load_failed_in_bg);
        this.addView(loadFailedView);
        ((FrameLayout.LayoutParams) loadFailedView.getLayoutParams()).gravity = Gravity.CENTER;
    }

    /**
     * 改变整个控件的可点击状态和点击事件，达到可以适时重新加载的目的
     * 
     * @param clickable
     */
    public void changeClickable(boolean clickable) {
        if (isTouchListenerValid) {
            changeTouchable(clickable);
        } else {
            if (clickable) {
                this.setClickable(true);
                this.setOnClickListener(layoutClickEvent);
            } else {
                this.setClickable(false);
                this.setOnClickListener(null);
            }
        }
    }

    private void changeTouchable(boolean touchable) {
        if (touchable) {
            this.setOnTouchListener(layoutTouchEvent);
        } else {
            this.setOnTouchListener(null);
        }
    }

    public void setTouchEventValid(boolean valid) {
        this.isTouchListenerValid = valid;
    }

    /**
     * 设置加载失败的图片
     * 
     * @param drawable
     *            Integer或者Drawable都可接受
     * 
     */
    public void setLoadFailedDrawable(Object drawable) {
        ImageView v = (ImageView) findViewById(R.id.image_v_load_failed);
        if (v == null)
            return;
        ViewUtils.setViewBackground(v, drawable);
    }

    /**
     * 加载成功，但是没有数据，这里会调用onLoadFailed()方法，并且设置提示文字
     */
    @Override
    public void onNoData() {
        onLoadFailed();
        setLoadFailedHint(R.string.wl_no_data);
    }

    /**
     * 因为onNoData()的实现机制，所以这里需要复写onLoadFailed方法，重新设置提示文字之后再调用父类的对应方法
     * 否则，下次调用onLoadFailed()方法时，实际上显示的是onNoData()的效果
     */
    @Override
    public boolean onLoadFailed() {
        setLoadFailedHint(R.string.wl_reload);
        return super.onLoadFailed();
    }

    /**
     * 设置加载失败的提示信息，默认是“点击屏幕 重新加载”
     * 
     * @param hint
     */
    public void setLoadFailedHint(String hint) {
        TextView view = (TextView) findViewById(R.id.txt_v_load_failed_tip);
        if (hint == null || view == null)
            return;
        view.setText(hint);
    }

    /**
     * 设置加载失败的提示信息，默认是“点击屏幕 重新加载”
     * 
     * @param stringId
     */
    public void setLoadFailedHint(int stringId) {
        String str = null;
        try {
            str = getResources().getString(stringId);
        } catch (NotFoundException e) {
        }
        setLoadFailedHint(str);
    }

    /**
     * 设置加载中的提示信息，默认是“努力加载中...”
     * 
     * @param hint
     */
    public void setLoadingHint(String hint) {
        TextView t = (TextView) findViewById(R.id.txt_v_loading_tip);
        if (t != null)
            t.setText(hint);
    }

    /**
     * 清空界面，界面效果与调用onSuccess()完全一样，但不是通过调用onSuccess()实现，因为onSuccess()中还包含了外部事件处理
     * 
     */
    public void clear() {
        setLoadingFlag(false);
        stopLoading(true);
    }
}
